package com.example.ruoyivoicecall.util;

import java.io.File;

/**
 * 路径工具类
 */
public class PathUtil {
    // 内部接口类用于兼容旧代码
    public static class NewInterface {
        public String getWebRoot() {
            return System.getProperty("user.dir") + File.separator + "webroot";
        }
    }

    /**
     * 获取新接口实例
     */
    public static NewInterface getNewInterface() {
        return new NewInterface();
    }
    /**
     * 获取项目根目录
     */
    public static String getProjectRoot() {
        return System.getProperty("user.dir");
    }

    /**
     * 获取文件上传目录
     */
    public static String getUploadPath() {
        return getProjectRoot() + File.separator + "upload";
    }

    /**
     * 获取语音文件目录
     */
    public static String getVoiceFilePath() {
        return getUploadPath() + File.separator + "voice";
    }

    /**
     * 确保目录存在
     */
    public static void ensureDirectoryExists(String directoryPath) {
        File directory = new File(directoryPath);
        if (!directory.exists()) {
            directory.mkdirs();
        }
    }

    /**
     * 构建文件路径
     */
    public static String buildFilePath(String... parts) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < parts.length; i++) {
            sb.append(parts[i]);
            if (i < parts.length - 1) {
                sb.append(File.separator);
            }
        }
        return sb.toString();
    }

    /**
     * 获取文件名（不含路径）
     */
    public static String getFileName(String filePath) {
        if (filePath == null) {
            return null;
        }
        int lastSeparatorIndex = filePath.lastIndexOf(File.separator);
        if (lastSeparatorIndex == -1) {
            return filePath;
        }
        return filePath.substring(lastSeparatorIndex + 1);
    }

    /**
     * 获取文件扩展名
     */
    public static String getFileExtension(String fileName) {
        if (fileName == null) {
            return null;
        }
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1) {
            return "";
        }
        return fileName.substring(dotIndex + 1).toLowerCase();
    }
}